var FileUploader = {

    id: 0,
    pull: [],

    create: function(hParams) {

        if (!hParams.box) this.p_error('need box');

        var form = this.p_createForm(hParams.box);
        var iframe = this.p_createIframe();

        form.action = hParams.url;
        form.target = iframe.name;

        this.lastId++;

        this.pull[this.lastId] = {
            'box': hParams.box,
            'onstart': hParams.onstart || null,
            'onfinish': hParams.onfinish || null,
            'url': hParams.url || './',
            'iframe': iframe,
            'form': form,
            'checkSelectedTimer': window.setInterval('FileUploader.p_checkSelected(' + this.lastId + ')',500)
        };
    },

    p_send: function (id) {

        var current = this.pull[id];
        current.form.submit();
        current.form.style.visibility = 'hidden';

        if (current.onstart != null)
            current.onstart(current.box);

        current.checkLoadedTimer = window.setInterval('FileUploader.p_checkLoaded(' + id + ')',2000);
    },

    p_checkLoaded: function(id) {

        var current = this.pull[id];
        var doc = this.p_getIframeDocument(current.iframe);

        if (doc.firstChild && doc.firstChild.innerHTML != '') {

            window.clearInterval(current.checkLoadedTimer);
            current.box.innerHTML = '';

            if (current.onfinish != null) {
                var firstChild = doc.firstChild;
                var bodyes = firstChild.getElementsByTagName && firstChild.getElementsByTagName('body');

                var response = (bodyes && bodyes[0] && bodyes[0].innerHTML) || firstChild.innerHTML;
                response = activateHTML(response);
                current.onfinish(response,current.box);
            }

            this.pull[id] = {};

            current.iframe.parentNode.removeChild( current.iframe );
        }
    },

    p_checkSelected: function(id) {
        var current = this.pull[id];
        if (current.form && current.form.file &&    current.form.file.value && current.form.file.value != '') {
            window.clearInterval(current.checkSelectedTimer);
            this.p_send(id);
        }
    },

    p_getIframeDocument: function(iframe) {
        var doc = iframe.contentDocument ||  iframe.contentWindow.document || this.p_error('can not get document');
        return doc;
    },

    p_createIframe: function() {

        var iframeName = 'FileUploaderIframe' + this.lastId;
        var container = document.createElement('div');

        with (container) {
            style.position = 'absolute';
            style.visibility = 'hidden';
            innerHTML =
              '<iframe src="" name="' + iframeName + '" style="width:0;height:0;border:0;overflow:hidden;"></iframe>';
//            '<iframe src="" name="' + iframeName + '" style="width:200px;height:200px;border:solid 1px green;overflow:hidden;"></iframe>';
        };

        var iframe = container.getElementsByTagName('iframe')[0];
        document.body.appendChild(container);

        return iframe;
    },

    p_createForm: function(box) {

        var formName = 'FileUploaderForm' + this.lastId;
        box.innerHTML = '<form style="margin:0" id="' + formName + '" method="post" enctype="multipart/form-data" ' +
            'onsubmit=""><input type="file" name="file"></form>';

        var form = document.getElementById(formName);

        return form;
    },

    p_error: function(msg) {
        alert(msg);
        //document.writeln(msg);
    }
}

function deactivateHTML(str) {
    for(var entity in deactivateHTML.replaceTable)
        str = str.replace(new RegExp(deactivateHTML.replaceTable[entity],'g'),entity);
    return str;
}

function activateHTML(str) {
    for(var entity in deactivateHTML.replaceTable)
        str = str.replace(new RegExp(entity,'g'),deactivateHTML.replaceTable[entity]);
    return str;
}

deactivateHTML.replaceTable = {
    '&amp;': '&',
    '&lt;': '<',
    '&gt;': '>'
}